package login.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import login.adm.cadastro.Usuario;
import login.generic.GenericDao;

public class UsuarioDao extends GenericDao {
	
	public UsuarioDao(){
		super();
	}
	
	public boolean efetuarlogin(Usuario usuario) {
		String query = "Select * from tb_usario where login = ? and senha = ?";				
		try {
			PreparedStatement stm = super.getConexao().prepareStatement(query);
			stm.setString(1,usuario.getLogin());
			stm.setString(2, usuario.getSenha());
			
			ResultSet resultado = stm.executeQuery();
			
			return resultado.next();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{super.fechaConexao();}
		return false;
	}
	
	public boolean saveUser(Usuario usuario){
		String query = "insert into tb+_usario(nome, login, senha) values (?, ?, ?)";
		
		try {
			PreparedStatement stm = super.getConexao().prepareStatement(query);
			stm.setString(1, usuario.getNome());
			stm.setString(2, usuario.getLogin());
			stm.setString(3, usuario.getSenha());
			
			int result = stm.executeUpdate();
			
			if (result != 0){
				return true;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{super.fechaConexao();} 
		
		return false;
	}
}
